//
//  Thirdlub_DiscussionCollectionViewCell.m
//  Project_B
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "Thirdlub_DiscussionCollectionViewCell.h"
#import "Thirdlub_DiscussionTableViewCell.h"
#import "Thirdlub_Discussion2TableViewCell.h"
#import "AFNetworkHandler.h"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"

#define CWIDTH [[UIScreen mainScreen] bounds].size.width
#define CHEIGHT [[UIScreen mainScreen] bounds].size.height

@interface Thirdlub_DiscussionCollectionViewCell () < UITableViewDataSource, UITableViewDelegate >
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, assign) NSInteger i;

@end

@implementation Thirdlub_DiscussionCollectionViewCell

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.i = 0;
        self.top = [NSMutableArray array];
        self.normal = [NSMutableArray array];
        self.table = [[UITableView alloc] initWithFrame:self.contentView.frame style:(UITableViewStyleGrouped)];
        self.table.dataSource = self;
        self.table.delegate = self;
        [self.contentView addSubview:self.table];
        [self.table registerClass:[Thirdlub_DiscussionTableViewCell class] forCellReuseIdentifier:@"cell1"];
        [self.table registerClass:[Thirdlub_Discussion2TableViewCell class] forCellReuseIdentifier:@"cell2"];
        [self setupRefresh];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    self.table.frame = CGRectMake(0, 0, CWIDTH, CHEIGHT - 190);
}

#pragma mark - 更新视图
-(void)reloadData{
    [self.table reloadData];
}
#pragma mark - 刷新数据
- (void)setupRefresh
{
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    self.table.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(prepareDiscussionData1)];
    
}
#pragma mark - 加载讨论区数据
- (void)prepareDiscussionData1
{
    self.i++;
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/topiclist/%@/0/?page=%ld?category=0&club_id=%@&page=%ld", self.club_id, self.i, self.club_id, self.i];
    
   [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
       NSArray *arr = result[@"data"][@"topiclist"];
       for (NSDictionary *dic in arr) {
        if ([dic[@"stick"] isEqualToString:@"2"]) {
                [self.normal addObject:dic];
        } else {
                [self.top addObject:dic];
            }
        }
       [self.table reloadData];
       [self.table.mj_footer endRefreshing];
   }];
}

#pragma mark - tableView协议方法
#pragma mark - 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
#pragma mark - 头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
#pragma mark - cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 45;
    } else {
        return 60;
    }
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.top.count;
    } else {
        return self.normal.count;
    }
}
#pragma mark - 单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Thirdlub_DiscussionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.dic = self.top[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//无选中颜色
        return cell;
    } else {
        Thirdlub_Discussion2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//无选中颜色
        cell.dic = self.normal[indexPath.row];
        return cell;
    }
}
#pragma mark - 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        [self.Delegate discussionJumpVC:self.top[indexPath.row][@"id"]];
    } else {
        [self.Delegate discussionJumpVC:self.normal[indexPath.row][@"id"]];
    }
}




@end
